﻿namespace PorteringManagement.Web.Controllers
{
    using System;
    using System.Linq;
    using System.Web.Mvc;
    using PorteringManagement.Model;

    [Authorize(Roles = "Admin")]
    public class ItineraryController : Controller
    {
        private PorteringManagementContext context = new PorteringManagementContext();

        // GET: /Itinerary/
        public ViewResult Index()
        {
            return this.View(this.context.Itineraries.ToList());
        }

        // GET: /Itinerary/Details/5
        public ViewResult Details(int id)
        {
            var itinerary = this.context.Itineraries.Find(id);
            foreach (var journey in itinerary.Journeys)
            {
                journey.DeparturePort = this.context.Ports.Find(journey.DeparturePortID);
                journey.ArrivalPort = this.context.Ports.Find(journey.ArrivalPortID);
            }

            return this.View(itinerary);
        }

        // GET: /Itinerary/Create
        public ActionResult Create()
        {
            this.ViewBag.PortID = new SelectList(this.context.Ports, "PortID", "Name");
            this.ViewBag.AnalysisTypes = Enum.GetValues(typeof(AnalysisType))
                .Cast<AnalysisType>()
                .Select(at => new SelectListItem { Text = GetDisplayText(at), Value = ((int)at).ToString() });
            
            return this.View();
        } 

        // POST: /Itinerary/Create
        [HttpPost]
        public ActionResult Create(Itinerary itinerary, AnalysisType analysisType)
        {
            if (this.ModelState.IsValid)
            {
                var portOrigin = this.context.Ports.SingleOrDefault(p => p.PortID == itinerary.DeparturePortID);
                var portDestination = this.context.Ports.SingleOrDefault(p => p.PortID == itinerary.ArrivalPortID);

                itinerary.Journeys = Itinerary.CalculareMinPath(portOrigin, new[] { portOrigin, portDestination }, analysisType);
                itinerary.Journeys.ToList().ForEach(j => this.context.Journeys.Add(j));

                this.context.Itineraries.Add(itinerary);
                this.context.SaveChanges();

                return this.RedirectToAction("Index");  
            }

            this.ViewBag.PortID = new SelectList(this.context.Ports, "PortID", "Name");
            this.ViewBag.AnalysisTypes = Enum.GetValues(typeof(AnalysisType))
                .Cast<AnalysisType>()
                .Select(at => new SelectListItem { Text = GetDisplayText(at), Value = ((int)at).ToString() });

            return this.View(itinerary);
        }
        
        // GET: /Itinerary/Delete/5
        public ActionResult Delete(int id)
        {
            var itinerary = this.context.Itineraries.Find(id);
            
            return this.View(itinerary);
        }

        // POST: /Itinerary/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var itinerary = this.context.Itineraries.Find(id);
            this.context.Itineraries.Remove(itinerary);
            this.context.SaveChanges();

            return this.RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            this.context.Dispose();
            base.Dispose(disposing);
        }

        private static string GetDisplayText(AnalysisType analysisType)
        {
            switch (analysisType)
            { 
                case AnalysisType.Time:
                    return "Menor Tiempo";
                case AnalysisType.Cost:
                    return "Menor Costo";
                case AnalysisType.Weather:
                    return "Mejor Clima";

                default: return "Menor Tiempo";
            }
        }
    }
}